﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;
using System.Data;
using System.Web.Security;
using vncom.Models;

namespace vncom.Controllers
{
    public class DanhmucsysController : Controller
    {
        //
        // GET: /Danhmuc/

        public ActionResult Index()
        {
            return View();
        }

        #region "Danh muc list"
        public ActionResult DanhmucsysList(string id)
        {
            List<SqlParameter> Pram = new List<SqlParameter>();
            SqlParameter M_TemplateID = new SqlParameter("@M_TemplateID", SqlDbType.VarChar);
            M_TemplateID.Value = id;
            Pram.Add(M_TemplateID);
            ViewData["M_TemplateID"] = id;
            SqlParameter HoatDong = new SqlParameter("@HoatDong", SqlDbType.VarChar);
            HoatDong.Value = "";
            Pram.Add(HoatDong);
            ViewData["TB_System_Lst"] = DataProvider.SelectStoreProcedure("TB_System_Lst", Pram);
            return View();
        }
        #endregion

        #region "Them moi danh muc"
        public ActionResult DanhmucsysAdd(string id)
        {
            DanhMucSYS_Edit DMSYS = new DanhMucSYS_Edit();
            DMSYS.M_TemplateID = int.Parse(id);
            DMSYS.DanhMucSYS_Lst = getDanhMucSys(id);
            return View(DMSYS);
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult DanhmucsysAdd(DanhMucSYS_Edit DMSYS, string SYS_DanhMuc, string hdTemplateID)
        {
            if (SYS_DanhMuc != null)
            {
                if (DMSYS.TBSystemID == null || DMSYS.TBSystemID == 0)
                {
                    var Pram = setDuLieu(DMSYS, SYS_DanhMuc, hdTemplateID);
                    DataProvider.NonqueryProc("TB_System_Ins", Pram);
                }
                else
                {
                    var Pram = setDuLieu(DMSYS, SYS_DanhMuc, hdTemplateID, DMSYS.TBSystemID.ToString());
                    DataProvider.NonqueryProc("TB_System_Upd", Pram);
                }
            }
            return RedirectToAction("DanhmucsysList", "Danhmucsys", new { id = hdTemplateID });

        }
        #endregion

        #region "Thay doi danh muc"
        public ActionResult DanhmucsysEdit(string id)
        {
            List<SqlParameter> Pram = new List<SqlParameter>();
            SqlParameter TBSystemID = new SqlParameter("@TBSystemID", SqlDbType.Int);
            TBSystemID.Value = id;
            Pram.Add(TBSystemID);
            var dt = DataProvider.SelectStoreProcedure("TB_System_Get", Pram);
            DanhMucSYS_Edit DMSYS = new DanhMucSYS_Edit();
            if (dt.Rows.Count > 0)
            {
                DMSYS.TBSystemID = int.Parse(dt.Rows[0]["TBSystemID"].ToString());
                DMSYS.M_TemplateID = int.Parse(dt.Rows[0]["M_TemplateID"].ToString());
                DMSYS.NameTable = dt.Rows[0]["NameTable"].ToString();
                DMSYS.HoatDong = (Boolean)dt.Rows[0]["HoatDong"];
            }
            DMSYS.DanhMucSYS_Lst = getDanhMucSys("");
            return View(DMSYS);
        }
        #endregion

        public List<SqlParameter> setDuLieu(DanhMucSYS_Edit DMSYS, string SYS_DanhMuc, string hdTemplateID)
        {
            List<SqlParameter> Pram1 = new List<SqlParameter>();
            SqlParameter DMControllerID = new SqlParameter("@DMControllerID", SqlDbType.Int);
            DMControllerID.Value = SYS_DanhMuc;
            Pram1.Add(DMControllerID);
            var DMCT = DataProvider.SelectStoreProcedure("TB_Controller_Get", Pram1);

            List<SqlParameter> Pram = new List<SqlParameter>();
            SqlParameter M_TemplateID = new SqlParameter("@M_TemplateID", SqlDbType.Int);
            M_TemplateID.Value = hdTemplateID;
            Pram.Add(M_TemplateID);
            SqlParameter NameTable = new SqlParameter("@NameTable", SqlDbType.VarChar);
            NameTable.Value = DMCT.Rows[0]["NameTable"];
            Pram.Add(NameTable);
            SqlParameter NameControl = new SqlParameter("@NameControl", SqlDbType.VarChar);
            NameControl.Value = DMCT.Rows[0]["NameControl"];
            Pram.Add(NameControl);
            SqlParameter HoatDong = new SqlParameter("@HoatDong", SqlDbType.Bit);
            HoatDong.Value = DMSYS.HoatDong;
            Pram.Add(HoatDong);
            return Pram;
        }

        public List<SqlParameter> setDuLieu(DanhMucSYS_Edit DMSYS, string SYS_DanhMuc, string hdTemplateID, string TBSysID)
        {
            List<SqlParameter> Pram1 = new List<SqlParameter>();
            SqlParameter DMControllerID = new SqlParameter("@DMControllerID", SqlDbType.Int);
            DMControllerID.Value = SYS_DanhMuc;
            Pram1.Add(DMControllerID);
            var DMCT = DataProvider.SelectStoreProcedure("TB_Controller_Get", Pram1);

            List<SqlParameter> Pram = new List<SqlParameter>();
            SqlParameter TBSystemID = new SqlParameter("@TBSystemID", SqlDbType.Int);
            TBSystemID.Value = TBSysID;
            Pram.Add(TBSystemID);
            SqlParameter M_TemplateID = new SqlParameter("@M_TemplateID", SqlDbType.Int);
            M_TemplateID.Value = hdTemplateID;
            Pram.Add(M_TemplateID);
            SqlParameter NameTable = new SqlParameter("@NameTable", SqlDbType.VarChar);
            NameTable.Value = DMCT.Rows[0]["NameTable"];
            Pram.Add(NameTable);
            SqlParameter NameControl = new SqlParameter("@NameControl", SqlDbType.VarChar);
            NameControl.Value = DMCT.Rows[0]["NameControl"];
            Pram.Add(NameControl);
            SqlParameter HoatDong = new SqlParameter("@HoatDong", SqlDbType.Bit);
            HoatDong.Value = DMSYS.HoatDong;
            Pram.Add(HoatDong);
            return Pram;
        }

        public List<DanhMucSYS_HT> getDanhMucSys(string idTemplate)
        {
            List<SqlParameter> Pram = new List<SqlParameter>();
            SqlParameter M_TemplateID = new SqlParameter("@M_TemplateID", SqlDbType.VarChar);
            M_TemplateID.Value = idTemplate;
            Pram.Add(M_TemplateID);
            List<DanhMucSYS_HT> HT_L = new List<DanhMucSYS_HT>();
            var dt = DataProvider.SelectStoreProcedure("TB_Controller_Lst", Pram);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DanhMucSYS_HT HT = new DanhMucSYS_HT();
                HT.NameID = dt.Rows[i]["DMControllerID"].ToString();
                HT.NameTable = dt.Rows[i]["NameTable"].ToString();
                HT_L.Add(HT);
            }
            return HT_L;
        }
    }
}
